package cn.com.cnxfs.myzjsu;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        final SharedPreferences sharedPreferences = getSharedPreferences("settings", MODE_PRIVATE);
        if(!sharedPreferences.getString("username","").equals("")){ //如果已经处于登录状态，则直接路过本页面
            startActivity(new Intent(this, MainActivity.class));
            finish();
            return;
        }

        final Button button = findViewById(R.id.button_login);
        button.setOnClickListener(view -> {  //登录按钮
            final EditText username = findViewById(R.id.username);
            final EditText password = findViewById(R.id.password);
            final String _username = username.getText().toString();
            final String _password = password.getText().toString();
            if(_username.equals(""))
                Toast.makeText(this, "用户名不能为空！", Toast.LENGTH_SHORT).show();
            else if(_password.equals(""))
                Toast.makeText(this, "密码不能为空！", Toast.LENGTH_SHORT).show();
            else {
                sharedPreferences.edit()
                        .putString("username", _username)
                        .putString("password", _password)
                        .apply();
                startActivity(new Intent(this, MainActivity.class));
                finish();
            }
        });
    }
}